<?php
namespace frontend\controllers;

use Yii;
use zhongkefu\zhongkefu;
use yii\web\Controller;
use yii\base\ErrorException;
use yii\web\NotFoundHttpException;
use Omnipay\Omnipay;
use yii\filters\AccessControl;

class UnionpayController extends Controller
{

    public $enableCsrfValidation = false;

    public function actionIndex(){
        $gateway    = Omnipay::create('UnionPay_Express');
        //$gateway->setEnvironment('environment');//environment or production
        $gateway->setMerId(Yii::$app->params['unionpaymerId']);
        $gateway->setCertPassword(Yii::$app->params['unionpaycertPassword']);

        $gateway->setCertDir(getcwd() . '/cert/');
        $gateway->setCertPath(getcwd() . '/cert'.'/acp_test_sign.pfx'); // .pfx file
        
        $gateway->setReturnUrl(Yii::$app->params['unionpayreturnUrl']);
        $gateway->setNotifyUrl(Yii::$app->params['unionpayreturnUrl']);

        $order = [
            'orderId'   => date('YmdHis'), //Your order ID
            'txnTime'   => date('YmdHis'), //Should be format 'YmdHis'
            'orderDesc' => 'My order title', //Order Title
            'txnAmt'    => '100', //Order Total Fee
        ];

        $response = $gateway->purchase($order)->send();

         $response->redirect(); //For PC/Wap
      //  $response->getTradeNo(); //For APP
    }
    public function actionNotify(){
        $gateway    = Omnipay::create('UnionPay_Express');
        $gateway->setMerId(Yii::$app->params['unionpaymerId']);
        $gateway->setCertDir(getcwd() . '/cert/'); //The directory contain *.cer files
        $response = $gateway->completePurchase(['request_params'=>$_REQUEST])->send();
        if ($response->isPaid()) {
            //pay success
        }else{
            //pay fail
        }
    }
    public function actionReturn(){
        $gateway    = Omnipay::create('UnionPay_Express');
        $gateway->setMerId(Yii::$app->params['unionpaymerId']);
        $gateway->setCertDir(getcwd() . '/cert/'); //The directory contain *.cer files
        $response = $gateway->completePurchase(['request_params'=>$_REQUEST])->send();
        if ($response->isPaid()) {
            //pay success
        }else{
            //pay fail
        }
    }

}